<style>
  .layui-input-inline{margin-top:10px;}
</style>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>数据分析</cite></a>
    <a><cite>商户分润日统计</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">商户分润日统计</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row" style="margin-bottom: 10px;">
            <div class="layui-form" style="float:right;">
              <div id="timeDiv" class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <a id="search" class="layui-btn search" style="margin-top: 10px" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
              </div>
            </div>
          </div>

          <table id="XxPay_Mch_Daily_Profit" lay-filter="XxPay_Mch_Daily_Profit"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script>
    layui.use(['table', 'util', 'timePick','admin'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,element = layui.element
            ,timePick = layui.timePick
            ,form = layui.form;

        timePick.initDate(-7,0);  //第一个参数表示开始时间   第二个表示结束时间   0,0今天   -1,-1昨天   -7,0近一周   以此类推
        element.render('breadcrumb', 'breadcrumb');

        var createTimeStart = $('#createTimeStart').val();
        var createTimeEnd = $('#createTimeEnd').val();

        var tplDate = function(d){
            return layui.util.toDateString(d.statisticsDate, "yyyy-MM-dd");
        };

        var tplRate = function(d){
            var rate = "";
            if (d.payTotalCount == 0) {
                rate = "0.00";
            }else {
                rate = parseFloat((d.paySuccessCount/d.payTotalCount)*100).toFixed(2);
            }
            var d = rate + "%";
            if(parseFloat(rate) >= 50.00) {
                return "<span style='color: green'>" + d + "</span>";
            }else if(parseFloat(rate) < 50.00) {
                return "<span style='color: red'>" + d + "</span>";
            }
        };

        // 统计列表
        table.render({
            elem: '#XxPay_Mch_Daily_Profit'
            ,cellMinWidth: 90
            ,url: layui.setter.baseUrl + '/data/mchDailyProfit'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token,
                createTimeStart: createTimeStart,
                createTimeEnd: createTimeEnd,
            }
            ,id: 'tableReload'
            ,page: true
            ,cols: [[
                {field: 'statisticsDate', title: '统计日期', templet: tplDate}
                ,{field: 'paySuccessAmount', title: '支付金额', sort: true, templet: '<div>{{ d.paySuccessAmount/100 }}</div>'}
                ,{field: 'payTotalFee', title: '支付手续费', sort: true, templet: '<div>{{ d.payTotalFee/100 }}</div>'}
                ,{field: 'paySuccessCount', title: '支付笔数', sort: true, templet: '<div>{{ d.paySuccessCount+"/"+d.payTotalCount }}</div>'}
                ,{field: 'paySuccessCount', title: '成功率', sort: true, templet: tplRate}
                ,{field: 'agentPaySuccessAmount', title: '代付金额', sort: true, templet: '<div>{{ d.agentPaySuccessAmount/100 }}</div>'}
                ,{field: 'agentPayTotalFee', title: '代付手续费', sort: true, templet: '<div>{{ d.agentPayTotalFee/100 }}</div>'}
                ,{field: 'agentPaySuccessCount', title: '代付笔数', sort: true}
                ,{field: 'rechargeSuccessAmount', title: '充值金额', sort: true, templet: '<div>{{ d.rechargeSuccessAmount/100 }}</div>'}
                ,{field: 'rechargeTotalFee', title: '充值手续费', sort: true, templet: '<div>{{ d.rechargeTotalFee/100 }}</div>'}
                ,{field: 'rechargeSuccessCount', title: '充值笔数', sort: true}
                ,{title: '总分润', sort: true, templet: '<div>{{ parseFloat((d.payTotalFee+d.agentPayTotalFee+d.rechargeTotalFee)/100).toFixed(2) }}</div>'}
            ]]
            ,skin: 'line'
        });

        // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var createTimeStart = $('#createTimeStart').val();
                var createTimeEnd = $('#createTimeEnd').val();
                var mchId = $('#mchId').val();
                //执行统计
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        createTimeStart: createTimeStart,
                        createTimeEnd: createTimeEnd,
                        mchId: mchId,
                    }
                });
            }
        };
        $('.search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 渲染表单
        form.render();
    });
</script>